//
//  WeatherDetailTableViewController.m
//  ErWa
//
//  Created by 李雪 on 16/3/16.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WeatherDetailTableViewController.h"
#import "NewNestHeader.h"

@interface WeatherDetailTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSArray *ary;
@property (nonatomic,strong)UILabel *cityLabel;
@property (nonatomic,strong)UILabel *weatherLabel;
@property (nonatomic,strong)UILabel *temLabel;

@end

@implementation WeatherDetailTableViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"%s",__FUNCTION__);
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    
    //解档获取数据
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    //2.拼接地址 (按照什么路径 保存为什么文件) (数组和字典写plist)
    NSString *strpath = [path stringByAppendingPathComponent:@"a.plist"];
    
    NSString *str = [NSKeyedUnarchiver unarchiveObjectWithFile:strpath];
    UIColor *bg = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
    //     判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        
        self.view.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        
    } else {
        self.view.backgroundColor = [UIColor clearColor];
        [self.view setBackgroundColor:bg];
        //导航栏按钮颜色
        self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        //Tabbar按钮颜色
        self.tabBarController.tabBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:str]];
        
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationItem.title = @"天气详情";
    self.navigationController.navigationBar.translucent = NO;
    
    [self deletTopWhite];
//    self.view.backgroundColor = [UIColor colorWithRed:176.0f/255.0f green:224.0f/255.0f blue:230.0f/255.0f alpha:0.8];
    self.view.backgroundColor = [UIColor clearColor];
    [self headView];
    [self getWeather];


    
  }

-(void)headView{
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"WeatherDetail" owner:self options:nil];
    

    UIView *temCustomView = [nib objectAtIndex:0];
    temCustomView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.05];
    
    temCustomView.layer.borderColor = [UIColor colorWithRed:219/255. green:219/255. blue:219/255. alpha:.8].CGColor;
    temCustomView.layer.borderWidth = 15;
    temCustomView.layer.cornerRadius = 5;
    temCustomView.clipsToBounds = YES;
    temCustomView.userInteractionEnabled = YES;
     

//    [self.view addSubview:temCustomView];
    
    _cityLabel =(UILabel *)[temCustomView viewWithTag:10];
    _weatherLabel = (UILabel *)[temCustomView viewWithTag:11];
    _temLabel = (UILabel *)[temCustomView viewWithTag:12];
    
    //设置成headView
    self.lastDayTabView = [[UITableView alloc]init];
    _lastDayTabView.tableHeaderView = temCustomView;
    
    self.lastDayTabView.frame = CGRectMake(0, 0, 375, 667);
    _lastDayTabView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.lastDayTabView];
    
    self.lastDayTabView.delegate = self;
    self.lastDayTabView.dataSource = self;
    
    // 添加 cell
    UINib *cellNib = [UINib nibWithNibName:@"cell" bundle:nil];
    [_lastDayTabView registerNib:cellNib forCellReuseIdentifier:@"mycellxib"];
    
//    UITableViewCell *cell = [CityWeatherTableViewCell dequeueReusableCellWithIdentifier:@"mycellxib" forIndexPath:indexPath];
    
}

// 获取URL获取天气信息
-(void)getWeather
{
    NSString *url = [NSString stringWithFormat:@"http://wthrcdn.etouch.cn/weather_mini?city=%@",_cityName];
    
    NSString *urll = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSLog(@"~~~~~%@",url);
    [AFNetworking GETWithURL:urll completion:^(id result) {
        // json-> dic
        NSDictionary *weatherDic = (NSDictionary *)result;
        // data dic
        NSDictionary *weatherInfo = [weatherDic objectForKey:@"data"];
        
        NSString *city = [weatherInfo objectForKey:@"city"];//
        _cityLabel.text = city;
        NSArray *weatherArray = [weatherInfo objectForKey:@"forecast"];
        _ary = weatherArray;
        NSDictionary *weather = [weatherArray objectAtIndex:0];
        NSLog(@"type = %@",[weather objectForKey:@"type"]);
        _weatherLabel.text = [weather objectForKey:@"type"];
        _temLabel.text = [NSString stringWithFormat:@"%@-%@",[weather objectForKey:@"low"],[weather objectForKey:@"high"]];
        [self.lastDayTabView reloadData];//更新数据
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
// section
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
// row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

// row high
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 40;
//}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIndentifier = @"cell";
    //从重用队列中取cell对象
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    //     判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        cell.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
    } else {
        cell.backgroundColor = [UIColor clearColor];
    }
    //取消点击cell变灰
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    //如果没有取出可以重用的cell,初始化
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellIndentifier];
    }
    if(_ary){
        NSDictionary *weather = [_ary objectAtIndex:indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"   %@-%@",[weather objectForKey:@"date"],[weather objectForKey:@"type"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@-%@   ",[weather objectForKey:@"low"],[weather objectForKey:@"high"]];
    }
    
    return cell;
}

-(void)deletTopWhite{
    float systemVersion = [[[UIDevice currentDevice]systemVersion] floatValue];
    if(systemVersion >7.0){
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}




@end
